<template>
    <el-form v-if="!registerState" class="sign-form" ref="signUpRef" :model="form" :rules="rules">
        <el-form-item class="sign-form-item" prop="name">
            <el-input class="sign-input" placeholder="用户名" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item class="sign-form-item" prop="password">
            <el-input class="sign-input" placeholder="密码" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item class="sign-form-item" prop="checkPass">
            <el-input class="sign-input" placeholder="确认密码" v-model="form.checkPass"></el-input>
        </el-form-item>
    </el-form>
    <template v-else>
        <div class="register-result">
            √
            <br>
            <br>
            注册成功
            <br>
            <br>
            <router-link to="/sign-in">去登录</router-link>
        </div>
    </template>
    <div v-if="!registerState" class="submit-warp">
        <el-button class="submit" type="primary"
            :loading="loading" @click="submit">
            注 册
        </el-button>
    </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import type { RegisterForm } from '@/apis/user/types'
import { register, checkUser } from '@/apis/user'
const registerState = ref<boolean>(false)
const signUpRef = ref<FormInstance>()

interface FormValue extends RegisterForm {
    checkPass: string
}
const form = reactive<FormValue>({
    name: '',
    password: '',
    checkPass: ''
})

// 自定义验证规则
const checkName = (rule: any, value: any, callback: any) => {
    if(!value) {
        callback(new Error('请输入用户名'))
    } else {
        checkUser(value).then(res => {
            if(!res.data) callback()
        }).catch((err: any) => {
            callback(new Error(err.message))
        })
    }
}
const checkPass = (rule: any, value: any, callback: any) => {
    if(!value) {
        callback(new Error('请输入密码'))
    } else {
        if(form.checkPass !== '') {
            signUpRef.value?.validateField('checkPass', () => null)
        }
        callback()
    }
}

const checkPassed = (rule: any, value: any, callback: any) => {
    if(!value) {
        callback(new Error('请再次输入密码'))
    } else if(value !== form.password) {
        callback(new Error('两次输入的密码不一致'))
    } else {
        callback()
    }
}

// 验证
const rules = reactive<FormRules>({
    name: [
        { validator: checkName, trigger: 'blur' },
        { required: true, message: '请输入用户名', trigger: 'blur' }
    ],
    password: [
        { validator: checkPass, trigger: 'blur' }
    ],
    checkPass: [
        { validator: checkPassed, trigger: 'blur' }
    ]
})

// 注册
const loading = ref<boolean>(false)
const submit = () => {
    signUpRef.value?.validate(async (valid) => {
        if(valid) {
            loading.value = true
            const { name, password } = form
            try {
                const res = await register({ name: name, password: password })
                if(res.code === 200) ElMessage.success('注册成功')
                loading.value = false
                registerState.value = true
            } catch(err) {
                loading.value = false
            }
        }
    })
}
</script>

<style lang="less" scoped>
@import './sign.less';
.test {
    color: var(--el-color-primary);
}
</style>